//
//  LJItemCell.m
//  LJAnimationsDemo
//
//  Created by raymond on 17/1/5.
//  Copyright © 2017年 raymond. All rights reserved.
//

#import "LJItemCell.h"
#import "LJAnimationItem.h"

@interface LJItemCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitlelLabel;

@end

@implementation LJItemCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.subTitlelLabel];
}

- (void)setItem:(LJAnimationItem *)item {
    _item = item;
    self.titleLabel.attributedText = item.nameStr;
    self.subTitlelLabel.text = NSStringFromClass(item.object);
    if (item.index % 2) {
        self.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.05f];
    } else {
        self.backgroundColor = [UIColor whiteColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    if (highlighted) {
        
    } else {
        CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        keyAnimation.values = @[@1.0, @1.1, @1.0];
        keyAnimation.duration = 0.6;
        keyAnimation.repeatCount = 1;
        keyAnimation.removedOnCompletion = YES;
        [self.titleLabel.layer addAnimation:keyAnimation forKey:@"scale"];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(10, 8, 290, 25);
    self.subTitlelLabel.frame = CGRectMake(10, 35, 290, 10);
}

#pragma mark - getter 

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
    }
    return _titleLabel;
}

- (UILabel *)subTitlelLabel {
    if (!_subTitlelLabel) {
        _subTitlelLabel = [[UILabel alloc] init];
        _subTitlelLabel.font = [UIFont fontWithName:@"Avenir-Light" size:8.f];
        _subTitlelLabel.textColor = [UIColor grayColor];
    }
    return _subTitlelLabel;
}

@end
